﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;

namespace WH
{
    
    public class WarehouseService : IWarehouseService
    {
        private SqlConnection connection;
        private SqlCommand command;
        private SqlDataReader reader;

        public Warehouse[] GetWarehouse() {

            List<Warehouse> list = new List<Warehouse>();

            openConnection("n7251998", "inb374");

            command = new SqlCommand("SELECT Name,Price, Quantity, Ordertime FROM Component", connection);

            reader = command.ExecuteReader();

            while (reader.Read()) {

                Warehouse wh = new Warehouse(reader.GetString(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetInt32(3));
                list.Add(wh);
            }

            reader.Close();
            connection.Close();

            return list.ToArray();
        }

        private void openConnection(String username, String password) {
            
            connection = new SqlConnection(
            "Data Source=fastapps04.qut.edu.au;Initial Catalog=" + username + ";User ID=" + username + ";Password=" + password);

            connection.Open();
        }
    }
}
